<?php
require_once('PHPMailer_v5.1/class.phpmailer.php');
include('Utils.php');
class CenterInformationsController extends AppController{
	/**
	 * List components used in this class
	 */
	public $components = array('CenterInfo', 'Subject', 'Course');

	
	/**
	 * Function used to display contact page.
	 */
	public function index(){
		// Get all subjects
		$subjects = $this->Subject->getListSubject();
		$this->set('subjects', $subjects);
		// Get common information
		$centerInfo = $this->CenterInfo->getInfo();
		$this->set('center_info', $centerInfo);
		$map_item = $centerInfo[0]["CenterInformation"]["m_cif_map_item"];
		$this->set('mapitem', html_entity_decode("$map_item"));
		
		// Get list Course
		$courses = $this->Course->getAll();
		$this->set('courses', $courses);
		
		$isMailSent = isset($_POST['isMailSent']) ? $_POST['isMailSent'] : "";
		If($isMailSent != ""){
			$name = isset($_POST["name"]) ? $_POST['name'] : "";
			$from = isset($_POST["email"]) ? $_POST['email'] : "";
			$to = isset($_POST["to"]) ? $_POST["to"] : "";
			$title = isset($_POST["subject"]) ? $_POST['subject'] : "";
			$content = isset($_POST["msg"]) ? $_POST['msg'] : '';
			if($name != "" && $from != "" && $to != "" && $title != "" && $content != ""){
				$this->set("message", $this->sendMail($name, $from, $title, $content, $to));
			}
		}
		

	}
	
	/**
	 * Function used to send mail to admin
	 */
	public function sendMail($senderName, $from, $title, $content, $to){
		
		$mail = new PHPMailer();  // create a new object
		$mail->IsSendmail();

		global $error;
		$mail = new PHPMailer();  // create a new object
		$mail->IsSMTP(); // enable SMTP
		$mail->CharSet = 'UTF-8';
		$mail->SMTPDebug = 1;  // debugging: 1 = errors and messages, 2 = messages only
		$mail->SMTPAuth = true;  // authentication enabled
		$mail->SMTPSecure = 'SSL'; // secure transfer enabled REQUIRED for GMail
		$mail->Host = 'ssl://smtp.gmail.com';
		$mail->Port = 465;
		$mail->Username = 'nguyenta108@gmail.com';
		$mail->Password = '10892089';
		$mail->SetFrom($from, "Viện Tin Học");
		$mail->AddReplyTo($from, $senderName);
		$mail->Subject = $title;
		$mail->Body = $this->createBody($senderName, $from, $content);
		$mail->AddAddress("nguyennt11@gmail.com");
		if(!$mail->Send()) {
			return 'Gửi mail thất bại. Vui lòng kiểm tra lại thông tin';
		} else {
			return "Cảm ơn bạn đã gửi mail cho chúng tôi.<br/> Chúng tôi sẽ trả lời trong thời gian sớm nhất.";
		}
		
	}
	
	/**
	 * Function used to create mail body
	 * @param String $name
	 * @param String $from
	 * @param String $content
	 */
	function createBody($name, $from, $content){
		$body = "Bạn đã nhận được thư mới từ " . $name . " (" . $from . ") với nội dung: \n";
		$body = $body . "\n";
		$body = $body . $content;
		$body = $body . "\n\n--------------------------\n";
		$body = $body . "Lưu ý: Đây là mail được gửi tự động từ website Viện Tin Học.";
		$body = $body . "Vui lòng reply về địa chỉ email của người gửi bên trên.";
		return $body;
	}
	
	/**
	 * Admin function used to display edit page
	 */
	public function edit(){
		
		// Check session and redirect to login page
		Utils::checkSession($this);
		$this->set('admin', '1');
		
		// Get common information
		$centerInfo = $this->CenterInfo->getInfo();
		$this->set('center_info', $centerInfo);
		$subjects = $this->Subject->getListSubject();
		$this->set('subjects', $subjects);
		
		$centerId = $centerInfo[0]["CenterInformation"]["m_cif_id"];
		
		/* In case back from confirm */
		$centerName = (isset($_POST['centerName'])) ? $_POST['centerName'] : $centerInfo[0]["CenterInformation"]["m_cif_name"];
		$centerAddress = (isset($_POST['centerAddress'])) ? $_POST['centerAddress'] : $centerInfo[0]["CenterInformation"]["m_cif_address"];
		$centerTel = (isset($_POST['centerTel'])) ? $_POST['centerTel'] : $centerInfo[0]["CenterInformation"]["m_cif_tel"];
		$centerMobile = (isset($_POST['centerMobile'])) ? $_POST['centerMobile'] : $centerInfo[0]["CenterInformation"]["m_cif_mobile"];
		$centerEmail = (isset($_POST['centerEmail'])) ? $_POST['centerEmail'] : $centerInfo[0]["CenterInformation"]["m_cif_email"];
		$centerMap = (isset($_POST['centerMap'])) ? $_POST['centerMap'] :  $centerInfo[0]["CenterInformation"]["m_cif_map_item"];
		
		$this->set('centerId', $centerId);
		$this->set('centerName', $centerName);
		$this->set('centerAddress', $centerAddress);
		$this->set('centerTel', $centerTel);
		$this->set('centerMobile', $centerMobile);
		$this->set('centerEmail', $centerEmail);
		$this->set('centerMap', $centerMap);
	}
	
	/**
	 * Confirm posted faculty information
	 */
	function info_confirm(){
		
		// Check session and redirect to login page
		Utils::checkSession($this);
		$this->set('admin', '1');
		
		$name = isset($_POST['centerName']) ? $_POST['centerName'] : "";
		$address = isset($_POST['centerAddress']) ? $_POST['centerAddress'] : "";
		$tel = isset($_POST['centerTel']) ? $_POST['centerTel'] : "";
		$mobile = isset($_POST['centerMobile']) ? $_POST['centerMobile'] : "";
		$email = isset($_POST['centerEmail']) ? $_POST['centerEmail'] : "";
		$id = isset($_POST['centerId']) ? $_POST['centerId'] : "";
		$map = isset($_POST['centerMap']) ? Utils::encodeString($_POST['centerMap']) : "";
		$map = htmlentities($map, ENT_QUOTES, 'utf-8');
		$centerInfo = $this->CenterInfo->getInfo();
		$this->set('center_info', $centerInfo);
		$subjects = $this->Subject->getListSubject();
		$this->set('subjects', $subjects);
		
		$this->set('centerId', $id);
		$this->set('centerName', $name);
		$this->set('centerAddress', $address);
		$this->set('centerTel', $tel);
		$this->set('centerMobile', $mobile);
		$this->set('centerEmail', $email);
		$this->set('centerMap', $map);
	}
	
	public function info_update(){
		if ($this->request->is('post')) {
			$name = isset($_POST['centerName']) ? $_POST['centerName'] : "";
			$address = isset($_POST['centerAddress']) ? $_POST['centerAddress'] : "";
			$tel = isset($_POST['centerTel']) ? $_POST['centerTel'] : "";
			$mobile = isset($_POST['centerMobile']) ? $_POST['centerMobile'] : "";
			$mail = isset($_POST['centerEmail']) ? $_POST['centerEmail'] : "";
			$centerMap = isset($_POST['centerMap']) ? html_entity_decode($_POST['centerMap']) : "";

			if($name == "" || $address == '' || $tel == "" || $mobile == '' || $mail == "" || $centerMap == ""){
				$this->redirect(array('action' => 'edit'));
			}
			
			$dataNeedToSave = array('CenterInformation.m_cif_name' => "'".$name."'",
					                'CenterInformation.m_cif_address' => "'".$address."'",
					                'CenterInformation.m_cif_tel' => "'".$tel."'",
					                'CenterInformation.m_cif_mobile' => "'".$mobile."'",
									'CenterInformation.m_cif_email' => "'".$mail."'",
									'CenterInformation.m_cif_map_item' => "'".$centerMap."'");
			
			
			$saveCondition = array('CenterInformation.m_cif_id' => $_POST['centerId']);
			if ($this->CenterInformation->updateAll($dataNeedToSave, $saveCondition)) {
				$this->redirect(array('action' => 'edit'));
			}
		}
	}
}
?>